<?php

include_once("interface.php");

class Sample extends Data
{
    private $fn = "/tmp/sample.data";
    public $title = "Sample Configuration";
    public $module = "sample";
    public $data;

    public function __construct() {
        $this->data = array();
    }

    //read the data from files and return a complete object
    function readDataFromSystem() {
        $filearray = file($this->fn);
        foreach ( $filearray as $arrval ) {
            $val = trim($arrval);
            $this->data['sampledata'] = $val;
        }
        error_log("Sample readData(): read from $this->fn");
        return $this->data;
    }

    //write the obj to the file
    function commitDataToSystem($obj) {
        $ret = 0;
        $retstr = "Successfully set sample data";
        $fd = fopen($this->fn, "w");
        $ret = fwrite($fd, $obj['sampledata']);
        fclose($fd);

        return $retstr;
    }

    function printModuleHTML() {
        $str = <<<END

    <form style="width: 50%;">
    <fieldset class="ui-widget-content">
    <legend>Sample</legend>
    Sample Data: <input id="sampledata" type=text size=15 />
    </fieldset>
    <br>
    </form>
    <button id="submit" class="ui-widget-button">Submit</button>
    <div id="info"></div>

END;

        return $str;
    }

    function printModuleScript() {
        $str = <<<END

<script type="text/javascript">
    //fill in the form using values received from server
    function writeDataToHTML(data) {
        $('#sampledata').val(data.sampledata);
    }

    //fill in the obj to be sent to the server using the form values
    function readDataFromHTML() {
        var data = Object();
        data.sampledata = $('#sampledata').val();
        return data;
    }

    function validate(obj) {
        var msg = null;
        if(obj.data == "") {
            msg = "Sample data cannot be empty"
        }
        return msg;
    }

    //called before the submit operation
    function beforeSendFunction(msg) {
        $("#info").html("Sending ... ");
    }

    //called when the submit operation succeeds
    function successFunction(msg) {
        $("#submit").html("Submit");
        $("#info").html("Success: " + msg);
    }

    //called when the submit operation fails
    function errorFunction(msg) {
        $("#info").html("Error: " + msg);
    }

</script>

END;
        return $str;
    }

}

?>
